/*
FindPanel.m - Copyright (c) 1992 NeXT Computer, Inc.

You may freely copy, distribute and reuse the code in this example.
NeXT Computer, Inc. disclaims any warranty of any kind, expressed or implied, 
as to its fitness for any particular use.
*/

#import	"FindPanel.h"
#import	"MainDelegate.h"
#import	"QueryFieldEditor.h"

@implementation FindPanel

- ping:sender
{
    [browser setDelegate:self];
    [browser setTarget:self];
    [browser setAction:@selector(browserHit:)];
    [findField setStringValue:""];
    [findField selectText:self];
    return self;
}

- makeKeyAndOrderFront:sender
{
    [self ping:sender];
    [super makeKeyAndOrderFront:sender];
    return self;
}

- takeStringValueFrom:sender
{
    [findField setStringValue:[sender stringValue]];
    return self;
}

- performFind:sender
{
    [findButton performClick:sender];
    return self;
}

- findPostings
{
    char		*theText;
    char		*theExpression;
    id			theQuery;
    IXPostingList	*postingList;
    IXRecordManager	*recordManager;

    // get the text from the form
    theText = (char *)[findField stringValue]; 
    theExpression = alloca(MAXPATHLEN); 

    //  generate a query expression
    sprintf(theExpression, "whole(%s parse(%s))", "TheContent", theText);

    // create a query object. the parser is the same as the one on the attribute.
    recordManager = (IXRecordManager *)[[NXApp delegate] recordManager];
    theQuery = [[IXAttributeQuery alloc] initQueryString:theExpression 
	andAttributeParser:[recordManager parserForAttributeNamed:"TheContent"]];

    // perform query
    postingList = [theQuery evaluateFor:recordManager];

    // find all discarded records
    if (postingSet) postingSet = [postingSet free];
    postingSet = [[IXPostingSet alloc] initWithPostingsIn:postingList];
    [postingList free]; 
    if (postingList = [recordManager discards])
    {
	[postingSet subtractPostingsIn:postingList];
	[postingList free]; 
    }

    return self;
}

- doFind:sender
{
    [self findPostings];
    [browser loadColumnZero];
    [findField selectText:self];
    return self;
}

- doRefine:sender
{
    IXPostingSet	*original;

    original = nil;
    if (postingSet)
    {
	original = postingSet;
	postingSet = nil;
    }

    [self findPostings];
    if (original)
    {
	[postingSet formIntersectionWithPostingsIn:original];
	[original free];
    }

    [browser loadColumnZero];
    [findField selectText:self];
    return self;
}

- browserHit:sender
{
    [[NXApp delegate] dateDidChange:[[browser matrixInColumn:0] selectedCell]];
    return self;
}

- (int)browser:b fillMatrix:m inColumn:(int)c
{
    int			count;
    int			row;
    char		*string;
    IXRecordManager	*recordManager;
    id			cell;

    if (b != browser) return 0;
    recordManager = nil;
    count = [postingSet count];
    for (row = 0; row < count; row++)
    {
	if (!(cell = [m cellAt:row :0]))
	{
	    [m insertRowAt:row];
	    cell = [m cellAt:row :0];
	}

	if (!recordManager)
	    recordManager = (IXRecordManager *)[[NXApp delegate] recordManager];

	string = NULL;
	[recordManager getStringValue:&string 
	    ofMessage:@selector(theDate) forRecord:[postingSet setPosition:row]];

	if (string)
	{
	    [cell setLoaded:YES];
	    [cell setStringValue:string];
	    free(string);
	}

	[cell setLeaf:YES];
    }

    return count;
}

- windowWillReturnFieldEditor:sender toObject:client
{
    id	 	recordManager;

    if (client != findField) return nil;
    if (escapeEditor == nil)
    {
	escapeEditor = [[QueryFieldEditor alloc] init];
	recordManager = (IXRecordManager *)[[NXApp delegate] recordManager];
	[escapeEditor setCursor:[recordManager cursorForAttributeNamed:"TheContent"]];
    }

    return escapeEditor;
}

@end
